package com.example.payment.scheduler;

import com.example.payment.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
public class PaymentScheduler {
    
    private static final Logger log = LoggerFactory.getLogger(PaymentScheduler.class);
    
    @Autowired
    private PaymentService paymentService;
    
    /**
     * 每分钟处理过期订单
     */
    @Scheduled(fixedRate = 60000) // 每分钟执行一次
    public void processExpiredOrders() {
        try {
            log.info("开始处理过期订单...");
            paymentService.processExpiredOrders();
            log.info("过期订单处理完成");
        } catch (Exception e) {
            log.error("处理过期订单失败", e);
        }
    }
    
    /**
     * 每天凌晨2点清理过期的支付记录
     */
    @Scheduled(cron = "0 0 2 * * ?")
    public void cleanExpiredRecords() {
        try {
            log.info("开始清理过期支付记录...");
            // 这里可以添加清理逻辑
            log.info("过期支付记录清理完成");
        } catch (Exception e) {
            log.error("清理过期支付记录失败", e);
        }
    }
} 